package com.itheima.service.Impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    /**
     * 查询所有部门
     * @return
     */
    @Override
    public List<Dept> selectAll() {
        return deptMapper.selectAll();
    }
    /**
     * 删除部门
     * @param id
     */
    @Override
    public void delete(Integer id) {
        deptMapper.deleteById(id);
    }
    /**
     * 新增部门
     * @param dept
     */
    @Override
    public void add(Dept dept) {
        //往dept部门表中添加数据
        //只有name有值，creat_time和update_time为null，所以需要给这两个值设置系统时间
        try {
            dept.setCreateTime(LocalDateTime.now());
            //update_time和create_time一样
            dept.setUpdateTime(dept.getCreateTime());
            deptMapper.insert(dept);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DuplicateKeyException("重复添加异常");
        } finally {
        }

    }
    /**
     * 根据id查询部门
     * @param id
     * @return
     */
    @Override
    public Dept selectById(Integer id) {
        return deptMapper.selectById(id);
    }
    /**
     * 修改部门
     *
     * @param dept
     * @return
     */
    @Override
    public Dept update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
        return dept;
    }

}
